<?php
// +----------------------------------------------------------------------
// | SparkERP 优秀的进销存系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://erp.sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\controller;

use app\BaseController;
use think\facade\Filesystem;
use think\File;

class Common extends BaseController
{
    /**
     * 图片上传
     */
    public function upload()
    {
        crossDomain();
        
        if (!request()->isPost()) {
            return json(dataReturn(-1, '请使用POST方式上传'));
        }

        // 获取上传文件
        $file = request()->file('file');
        if (empty($file)) {
            return json(dataReturn(-1, '请选择要上传的文件'));
        }

        // 验证文件类型
        $allowedTypes = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
        $fileExt = strtolower($file->getOriginalExtension());
        if (!in_array($fileExt, $allowedTypes)) {
            return json(dataReturn(-1, '只支持 JPG、JPEG、PNG、GIF、WEBP 格式的图片'));
        }

        // 验证文件大小 (10MB)
        $maxSize = 10 * 1024 * 1024; // 10MB
        if ($file->getSize() > $maxSize) {
            return json(dataReturn(-1, '文件大小不能超过 10MB'));
        }

        try {
            // 按日期创建目录
            $date = date('Ymd');
            $uploadPath = 'uploads/images/' . $date;
            
            // 确保目录存在
            $fullPath = app()->getRootPath() . 'public/' . $uploadPath;
            if (!is_dir($fullPath)) {
                mkdir($fullPath, 0755, true);
            }

            // 生成唯一文件名
            $ext = strtolower(pathinfo($file->getOriginalName(), PATHINFO_EXTENSION));
            $fileName = time() . '_' . uniqid() . '.' . $ext;
            
            // 移动文件
            $file->move($fullPath, $fileName);
            
            // 构建访问URL
            $url = '/' . $uploadPath . '/' . $fileName;
            
            $result = [
                'fileName' => $fileName,
                'src' => request()->domain() . $url,
                'path' => $uploadPath . '/' . $fileName
            ];

            return json(dataReturn(0, '上传成功', $result));

        } catch (\Exception $e) {
            return json(dataReturn(-1, '上传失败：' . $e->getMessage()));
        }
    }

    /**
     * 文件上传（通用文件上传，支持各种文件类型）
     */
    public function uploadFile()
    {
        crossDomain();
        
        if (!request()->isPost()) {
            return json(dataReturn(-1, '请使用POST方式上传'));
        }

        // 获取上传文件
        $file = request()->file('file');
        if (empty($file)) {
            return json(dataReturn(-1, '请选择要上传的文件'));
        }

        // 验证文件类型
        $allowedTypes = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt', 'zip', 'rar', '7z', 'jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'avi', 'mov'];
        $fileExt = strtolower($file->getOriginalExtension());
        if (!in_array($fileExt, $allowedTypes)) {
            return json(dataReturn(-1, '不支持的文件类型，只支持: ' . implode('、', $allowedTypes)));
        }

        // 验证文件大小 (50MB)
        $maxSize = 50 * 1024 * 1024; // 50MB
        if ($file->getSize() > $maxSize) {
            return json(dataReturn(-1, '文件大小不能超过 50MB'));
        }

        try {
            // 按日期创建目录
            $date = date('Ymd');
            $uploadPath = 'uploads/files/' . $date;
            
            // 确保目录存在
            $fullPath = app()->getRootPath() . 'public/' . $uploadPath;
            if (!is_dir($fullPath)) {
                mkdir($fullPath, 0755, true);
            }

            // 生成唯一文件名
            $ext = strtolower(pathinfo($file->getOriginalName(), PATHINFO_EXTENSION));
            $fileName = time() . '_' . uniqid() . '.' . $ext;
            
            // 移动文件
            $file->move($fullPath, $fileName);
            
            // 构建访问URL
            $url = '/' . $uploadPath . '/' . $fileName;
            
            $result = [
                'fileName' => $fileName,
                'src' => request()->domain() . $url,
                'path' => $uploadPath . '/' . $fileName,
                'originalName' => $file->getOriginalName(),
                'size' => $file->getSize()
            ];

            return json(dataReturn(0, '上传成功', $result));

        } catch (\Exception $e) {
            return json(dataReturn(-1, '上传失败：' . $e->getMessage()));
        }
    }
}